<template>
	<view>
		<!--<import src="/remao_shop/component/noData/noData"></import>-->
		<view v-for="(item, index) in getList" :key="index">
			<view class="contre">
				<!-- 背景图片 -->
				<image class="bgtp" :src="item.status == 2 ? '../../img/youxiao_bj.png' : '../../img/youxiao_bj2.png'"></image>
				<!-- 数据 -->
				<form action report-submit="true" @submit="submit">
					<view class="contre-con">
						<view class="contre-shuju">
							<view class="money">
								<text>￥</text>{{item.discount}}</view>
							<view class="contre-name">
								<view class="name">{{item.name}}</view>
								<view class="name">满{{item.min_amount}}可用</view>
							</view>

							<button class="liji" v-if="item.status == 2" formType="submit" @tap="draw" :data-gid="index">立即领取</button>
							<button class="yijing" v-if="item.status == 1" formType="submit" @tap="draw" :data-gid="index">已领取</button>

						</view>
						<!-- 详情 -->
						<view class="contre-time">有效日期：{{item.start_date}}--{{item.end_date}}</view>
						<view class="contre-time" v-if="item.remark.length != 0">使用说明：{{item.remark}}</view>
					</view>
				</form>
			</view>
		</view>
		<no-data v-if="show"></no-data>
	</view>
</template>

<script>
	var app = getApp();
	import api from "../../../api";
	import noData from '../../component/noData/noData.vue'
	export default {
		data() {
			return {
				getList: [],
				cat_id: '',
				uid: '',
				coupon_id: '',
				show: false,
				status: ""
			};
		},

		components: {
			noData
		},
		props: {},

		/**
		 * 生命周期函数--监听页面加载
		 */
		onLoad: function() {
			wx.getStorage({
				key: 'userId',
				success: res => {
					console.log(res);
					this.setData({
						uid: res.data
					});
					this.getListFun();
				}
			});
		},

		/**
		 * 生命周期函数--监听页面初次渲染完成
		 */
		onReady: function() {},

		/**
		 * 生命周期函数--监听页面显示
		 */
		onShow: function() {},

		/**
		 * 生命周期函数--监听页面隐藏
		 */
		onHide: function() {},

		/**
		 * 生命周期函数--监听页面卸载
		 */
		onUnload: function() {},

		/**
		 * 页面相关事件处理函数--监听用户下拉动作
		 */
		onPullDownRefresh: function() {
			this.getListFun();
			wx.stopPullDownRefresh();
		},

		/**
		 * 页面上拉触底事件的处理函数
		 */
		onReachBottom: function() {},

		/**
		 * 用户点击右上角分享
		 */
		onShareAppMessage: function() {},
		methods: {
			getListFun: function() {
				app.globalData.request({
					url: api.default.coupon_list,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						uid: this.uid,
						page: 1,
						cat_id: ''
					},
					method: 'POST',
					success: res => {
						console.log(res); // console.log(res.coupon)

						this.setData({
							getList: res.coupon
						});

						if (res.msg == '暂无可领取的优惠券') {
							this.setData({
								show: true
							});
						} else {
							this.setData({
								show: false
							});
						}
					}
				});
			},
			drawList: function() {
				app.globalData.request({
					url: api.default.coupon_draw,
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						uid: this.uid,
						coupon_id: this.coupon_id
					},
					method: 'POST',
					success: res => {
						console.log(res);
						this.setData({
							status: res.status
						});
						wx.showToast({
							title: res.msg
						});
					}
				});
			},
			draw: function(event) {
				console.log(event.currentTarget.dataset.gid);
				console.log(this.getList[event.currentTarget.dataset.gid]);
				this.coupon_id = this.getList[event.currentTarget.dataset.gid].id;
				this.drawList();
				this.getListFun();
			},

			submit(e) {
				console.log(e);
				var a = e.detail.formId;
				wx.getStorage({
					key: 'userId',
					success: function(aaa) {
						console.log(aaa.data);
						var open = aaa.data;
						console.log(open);

						if (a == 'the formId is a mock one') {
							wx.showToast({
								title: '请用手机测试'
							});
						} else {
							console.log(a);
							app.globalData.request({
								url: api.gzSea.test,
								//仅为示例，并非真实的接口地址
								data: {
									form_id: a,
									user_id: open
								},
								method: 'POST',
								header: {
									'content-type': 'application/x-www-form-urlencoded' // 默认值

								},

								success(res) {
									console.log(res);
								}

							});
						}
					}
				});
			}

		}
	};
</script>
<style>
	@import "./coupon.css";
</style>
